package com.ruoyi.monitormanage.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.Tool;
import com.ruoyi.monitormanage.domain.TTestpointanddept;
import com.ruoyi.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.monitormanage.mapper.TTestpointMapper;
import com.ruoyi.monitormanage.domain.TTestpoint;
import com.ruoyi.monitormanage.service.ITTestpointService;

/**
 * monitormanagebuildService业务层处理
 *
 * @author xiaodabao
 * @date 2025-06-06
 */
@Service
public class TTestpointServiceImpl implements ITTestpointService
{
    @Autowired
    private TTestpointMapper tTestpointMapper;

    /**
     * 查询monitormanagebuild
     *
     * @param id monitormanagebuild主键
     * @return monitormanagebuild
     */
    @Override
    public TTestpointanddept selectTTestpointById(Long id)
    {
        return tTestpointMapper.selectTTestpointById(id);
    }

    /**
     * 查询monitormanagebuild列表
     *
     * @param tTestpoint monitormanagebuild
     * @return monitormanagebuild
     */
    @Override
    public List<TTestpointanddept> selectTTestpointList(TTestpoint tTestpoint)
    {
        return tTestpointMapper.selectTTestpointList(tTestpoint);
    }

    /**
     * 新增monitormanagebuild
     *
     * @param tTestpoint monitormanagebuild
     * @return 结果
     */
    @Override
    public int insertTTestpoint(TTestpoint tTestpoint)
    {
        tTestpoint.setToken(Tool.getUUID());
        return tTestpointMapper.insertTTestpoint(tTestpoint);
    }

    @Override
    public boolean checkTestpointUnique(TTestpoint tTestpoint) {
        Long deptId = StringUtils.isNull(tTestpoint.getId()) ? -1L : tTestpoint.getId();
        TTestpointanddept info = tTestpointMapper.checkTestpointUnique(tTestpoint.getYear(), tTestpoint.getOrgId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != deptId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;

    }

    /**
     * 修改monitormanagebuild
     *
     * @param tTestpoint monitormanagebuild
     * @return 结果
     */
    @Override
    public int updateTTestpoint(TTestpoint tTestpoint)
    {
        return tTestpointMapper.updateTTestpoint(tTestpoint);
    }

    /**
     * 批量删除monitormanagebuild
     *
     * @param ids 需要删除的monitormanagebuild主键
     * @return 结果
     */
    @Override
    public int deleteTTestpointByIds(Long[] ids)
    {
        return tTestpointMapper.deleteTTestpointByIds(ids);
    }

    /**
     * 删除monitormanagebuild信息
     *
     * @param id monitormanagebuild主键
     * @return 结果
     */
    @Override
    public int deleteTTestpointById(Long id)
    {
        return tTestpointMapper.deleteTTestpointById(id);
    }

    @Override
    public boolean importtestpoints(TTestpoint tTestpoint) {
        String yearNew = new SimpleDateFormat("yyyy").format(new Date());
        String yearOld = String.valueOf(Integer.parseInt(yearNew)-1);
        tTestpoint.setYear(yearOld);
        try{
            List<TTestpointanddept> TestpointanddeptList = tTestpointMapper.selectTTestpointList(tTestpoint);
            for (TTestpointanddept m : TestpointanddeptList) {
                m.setYear(yearNew);
                m.setIsPrevious("1");
                m.setIsCheck("1");
                m.setIsCheckRetest("1");
            }
            int i = tTestpointMapper.batchInsertTestpoint(TestpointanddeptList);
        }catch(Exception e){
            return false;
        }
        return true;
    }

    @Override
    public List<String> selectyearlist(TTestpoint tTestpoint) {
        return tTestpointMapper.selectyearlist(tTestpoint);
    }
}
